﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Lb.Common;

namespace Lb.ZipBak
{
    public partial class Settings : Form
    {
        public Settings()
        {
            this.InitializeComponent();
        }


        /// <summary>
        /// Setting Directory
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_SetDirectory_Click(object sender, EventArgs e)
        {
            if (this.GetDirPathDialog.ShowDialog() == DialogResult.OK)
            {
                this.txtSaveDirectory.Text = this.GetDirPathDialog.SelectedPath;
            }
        }

        /// <summary>
        /// save
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveConfig_Click(object sender, EventArgs e)
        {
            SettingConfig configinfo = new SettingConfig()
            {
                BackDirectoryPath = this.txtSaveDirectory.Text,
                TimeInterval = this.txtTimeInterval.Text,
                SyncDate = this.txtStartDate.Value.ToString("yyyy-MM-dd HH:mm:ss"),
                NameRegulation = this.txtCreateNameRegulation.Text,
                IsNewFolder = this.ckIsNewFolder.Checked,
                BackupDbYear = this.cbBakYear.SelectedIndex,
                OldDirectory = this.txtOldDirectory.Text,
                Extension = this.txtExtension.Text,
                ClearDay = (int)this.cbClearMonth.SelectedValue
            };

            XmlHelper.XmlSerialize<SettingConfig>(SystemParam.ConfigFilePath, configinfo);
            if (MessageBox.Show("保存成功,点击是后，窗口关闭。", "系统提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                Application.Exit();
            }

        }

        /// <summary>
        /// Cancel Exit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("取消操作后，窗口将关闭，是否取消操作？", "系统提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void Settings_Load(object sender, EventArgs e)
        {
            this.cbClearMonth.DataSource = BindComboxEnumType<ClearType>.BindTyps;
            this.cbClearMonth.DisplayMember = "Name";
            this.cbClearMonth.ValueMember = "Type";
            this.cbBakYear.SelectedIndex = 1;
            this.AssertExistsXmlConfig();
            SettingConfig config = XmlHelper.XmlDeserialize<SettingConfig>(SystemParam.ConfigFilePath);
            this.txtCreateNameRegulation.Text = config.NameRegulation;
            this.txtSaveDirectory.Text = config.BackDirectoryPath;
            this.txtTimeInterval.Text = config.TimeInterval;
            this.ckIsNewFolder.Checked = config.IsNewFolder;
            this.txtOldDirectory.Text = config.OldDirectory;
            this.txtExtension.Text = config.Extension;
            this.cbClearMonth.SelectedValue = (ClearType)config.ClearDay;
            this.cbBakYear.SelectedIndex = config.BackupDbYear;


            this.txtStartDate.Value = config.SyncDate.ObjToDateTime();
        }

        private void AssertExistsXmlConfig()
        {
            if (!File.Exists(SystemParam.ConfigFilePath))
            {
                SettingConfig configinfo = new SettingConfig()
                {
                    BackDirectoryPath = "d:\\FileBak",
                    TimeInterval = "00天00小时01分钟",
                    SyncDate = DateTime.Now.AddHours(1).ToString("yyyy-MM-dd HH:mm:ss"),
                    NameRegulation = "{文件名}-yyyyMMddHHmmssfff",
                    IsNewFolder = true,
                    BackupDbYear = 1,
                    Extension = "*.bak",
                    OldDirectory = "",
                    ClearDay = 0


                };
                XmlHelper.XmlSerialize<SettingConfig>(SystemParam.ConfigFilePath, configinfo);
            }
        }


        private void btnSetOldDriectory_Click(object sender, EventArgs e)
        {

            if (this.GetDirPathDialog.ShowDialog() == DialogResult.OK)
            {
                this.txtOldDirectory.Text = this.GetDirPathDialog.SelectedPath;
            }
        }
    }
}
